package com.cheng.student.controller.servlet;

import com.cheng.student.service.StudentServiceImpl;
import com.cheng.student.service.TeacherServiceImpl;
import com.cheng.student.service.interfaces.StudentService;
import com.cheng.student.service.interfaces.TeacherService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "DeleteStudentServlet", value = "/teacher/DeleteStudentServlet")
public class DeleteStudentServlet
        extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        String name = request.getParameter("name");
        String examNumber = request.getParameter("examNumber");

        if(name == null || examNumber == null
        || "".equals(name)|| "".equals(examNumber))
        {
            response.getWriter().write("信息错误，请核对");
            return;
        }

        StudentService service = new StudentServiceImpl();

        try
        {
            int i = service.deleteStudent(name, examNumber);
            if (i == 0)
            {
                throw new Exception();
            }
        }
        catch (Exception exception)
        {
            response.getWriter().write("删除失败，未知原因");
            return;
        }
        response.getWriter().write("删除成功，恭喜你");
    }
}
